package org.study.stream;

import com.google.common.collect.Lists;

import java.util.List;
import java.util.concurrent.*;

/**
 * parallelStream() 示例
 * 参考 https://blog.csdn.net/weixin_38845058/article/details/127070935
 *
 * @author Administrator
 * @date 2022-12-30
 */
public class StreamTest06 {
    public static void main(String[] args) throws InterruptedException {
        test01();
        test02();
    }

    public static void test01() throws InterruptedException {
        int processors = Runtime.getRuntime().availableProcessors();
        List<Integer> lists = Lists.newArrayList();
        for (int i = 0; i < processors; i++) {
            lists.add(i);
        }
        System.out.println("processors=" + processors + "，lists=" + lists.size());

        long start = System.currentTimeMillis();
        CountDownLatch countDownLatch = new CountDownLatch(3);

        new Thread(() -> {
            ForkJoinPool forkJoinPool = new ForkJoinPool(processors);
            forkJoinPool.submit(() -> {
                lists.parallelStream().forEach(e -> {
                    try {
                        TimeUnit.SECONDS.sleep(10);
                    } catch (InterruptedException e1) {
                        e1.printStackTrace();
                    }
                });
                System.out.println("执行1down");
                countDownLatch.countDown();
            });
        }).start();

        new Thread(() -> {
            ForkJoinPool forkJoinPool = new ForkJoinPool(processors);
            forkJoinPool.submit(() -> {
                lists.parallelStream().forEach(e -> {
                    try {
                        TimeUnit.SECONDS.sleep(10);
                    } catch (InterruptedException e1) {
                        e1.printStackTrace();
                    }
                });
                System.out.println("执行2down");
                countDownLatch.countDown();
            });
        }).start();

        new Thread(() -> {
            ForkJoinPool forkJoinPool = new ForkJoinPool(processors);
            forkJoinPool.submit(() -> {
                lists.parallelStream().forEach(e -> {
                    try {
                        TimeUnit.SECONDS.sleep(10);
                    } catch (InterruptedException e1) {
                        e1.printStackTrace();
                    }
                });
                System.out.println("执行3down");
                countDownLatch.countDown();
            });
        }).start();

        countDownLatch.await();
        System.out.println("总计耗时(ms)：" + (System.currentTimeMillis() - start));
    }

    public static void test02() throws InterruptedException {
        int processors = Runtime.getRuntime().availableProcessors();
        List<Integer> lists = Lists.newArrayList();
        for (int i = 0; i < processors; i++) {
            lists.add(i);
        }
        System.out.println("processors=" + processors + "，lists=" + lists.size());

        long start = System.currentTimeMillis();
        CountDownLatch countDownLatch = new CountDownLatch(3);

        new Thread(() -> {
            lists.parallelStream().forEach(e -> {
                try {
                    TimeUnit.SECONDS.sleep(10);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
            });
            System.out.println("执行1down");
            countDownLatch.countDown();
        }).start();

        new Thread(() -> {
            lists.parallelStream().forEach(e -> {
                try {
                    TimeUnit.SECONDS.sleep(10);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }

            });
            System.out.println("执行2down");
            countDownLatch.countDown();
        }).start();

        new Thread(() -> {
            lists.parallelStream().forEach(e -> {
                try {
                    TimeUnit.SECONDS.sleep(10);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
            });
            System.out.println("执行3down");
            countDownLatch.countDown();
        }).start();

        countDownLatch.await();
        System.out.println("总计耗时(ms)：" + (System.currentTimeMillis() - start));
    }
}
